package com.lb.shopguide.manager.service.update;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lb.shopguide.R;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.UpdateInfoBean;


/**
 * 更新对话框
 */
public class LbUpdateDialog extends AlertDialog {
    private Context mContext;

    private Builder builder;

    private View view;
    private TextView tvContent;

    private UpdateInfoBean updateBean;
    private UserConfigManager mUserConfigManager;

    private ButtonClickListener mButtonClickListener;

    public LbUpdateDialog(Context context, Bundle bundle) {
        super(context);
        initVariable(context, bundle);
        initView();
        initAdapter();
        bindData();
        initListener();
    }

    public void initVariable(Context context, Bundle bundle) {
        mContext = context;
        builder = new Builder(mContext);
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        updateBean = (UpdateInfoBean) bundle.getSerializable(AppConstant.UPDATE_BEAN);
        if (updateBean.isForce()) {
            builder.setCancelable(false);
        }
    }

    public void initView() {
        view = LayoutInflater.from(mContext).inflate(R.layout.dialog_update, null);
        tvContent = view.findViewById(R.id.tvContent);
        if (updateBean.isForce()) {
            builder.setTitle("发现新版本(强制更新)");
        } else {
            builder.setTitle("发现新版本");
        }
        tvContent.setText(Html.fromHtml(updateBean.getVersionDesc()));
        builder.setView(view);
    }

    public void initListener() {
        builder.setPositiveButton("更新版本", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mButtonClickListener.onSureClick(LbUpdateDialog.this, updateBean.getDownloadUrl(), "" + updateBean.getAppVersion());
            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mButtonClickListener.onCancelClick(LbUpdateDialog.this, updateBean.isForce());
            }
        });
        if (!updateBean.isForce()) {
            builder.setNeutralButton("不再提醒", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mUserConfigManager.setShowUpdate(false);
                    mUserConfigManager.setUpdateVersion(updateBean.getAppVersion());
                    mUserConfigManager.save2Sp(true);
                    mButtonClickListener.onNoMoreClick(LbUpdateDialog.this);
                }
            });
        }
    }

    public void initAdapter() {

    }

    public void bindData() {
        tvContent.setText(Html.fromHtml(updateBean.getVersionDesc()));
    }

    public void showBuilder() {
        builder.show();
    }

    public Builder getBuilder() {
        return builder;
    }

    public void setOnButtonClickListener(ButtonClickListener listener) {
        this.mButtonClickListener = listener;
    }

    public interface ButtonClickListener {
        void onSureClick(LbUpdateDialog dialog, String url, String versionCode);

        void onCancelClick(LbUpdateDialog dialog, boolean force);

        void onNoMoreClick(LbUpdateDialog dialog);
    }
}
